<?php
/**
 * @link https://www.kancloud.cn/cleverstone/ymb2
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\widget;

use yii\base\InvalidArgumentException;

/**
 * 交互组件门面类
 * @author cleverstone
 * @since ym2.0
 */
class WidgetFacade
{
    /**
     * 解决yii-bootstrap4部分小组件只能输出无法返回HTML内容
     * ```php
     * WidgetFacade::echo2Return(function () {
     *          \yii\bootstrap4\Modal::$autoIdPrefix = 'ModalB4';
     *          \yii\bootstrap4\Modal::begin([
     *                  'title' => 'ModalB4 title',
     *                  'bodyOptions' => [
     *                      'class' => 'text-center',
     *                  ],
     *                  'toggleButton' => [
     *                      'label' => 'ModalB4 btn',
     *                      'class' => 'layui-btn layui-btn-sm layui-btn-primary',
     *                  ],
     *          ]);
     *          echo implode("\n", array_fill(0, 100, 'ModalB4 content'));
     *          \yii\bootstrap4\Modal::end();
     * }),
     * ```
     * @param callable $closure
     * @return string
     * @throws \Throwable
     */
    public static function echo2Return($closure)
    {
        if (!is_callable($closure)) {
            throw new InvalidArgumentException('Except the param `$closure` is callback, But others be given. ');
        }

        return Echo2Return::widget([
            'widget' => $closure,
        ]);
    }
}